package com.itheima.d7_methodreference;

/**
 * 构造器的方法引用 使用条件   1.如果 -> 前后的参数一致
 *                        2.只是在创建对象 new
 *
 */
public class Demo4 {
    public static void main(String[] args) {
        //匿名内部类
        CarFactory cf = new CarFactory() {
            @Override
            public Car getCar(String name) {
                return new Car(name);
            }
        };

        //Lambda表达式
        CarFactory cf2 =  name -> new Car(name);
        //构造器的方法引用 类名::new
        CarFactory cf3 = Car::new;


        Car c1 = cf.getCar("法拉利");
        Car c2 = cf2.getCar("保时捷");
        Car c3 = cf3.getCar("兰博基尼");
        System.out.println(c1.getName() + " " + c2.getName() + " " + c3.getName());
    }
}


@FunctionalInterface //函数式接口
interface CarFactory {
    Car getCar(String name);
}
// 创建一个汽车类
class Car{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Car(String name) {
        this.name = name;
    }

    public Car() {
    }
}
